package com.example.wechat.adapter;

import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.wechat.R;
import com.example.wechat.model.FavoriteArticle;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class FavoritesArticleAdapter extends RecyclerView.Adapter<FavoritesArticleAdapter.ViewHolder> {
    private List<FavoriteArticle> articles;
    private SimpleDateFormat dateFormat;

    public FavoritesArticleAdapter(List<FavoriteArticle> articles) {
        this.articles = articles;
        this.dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_favorite_article, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        FavoriteArticle article = articles.get(position);
        holder.titleTextView.setText(article.getTitle());
        holder.summaryTextView.setText(article.getSummary());
        holder.timeTextView.setText(dateFormat.format(new Date(article.getTime())));

        holder.itemView.setOnClickListener(v -> {
            // 打开文章链接
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(article.getUrl()));
            v.getContext().startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return articles.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView titleTextView;
        TextView summaryTextView;
        TextView timeTextView;

        ViewHolder(View itemView) {
            super(itemView);
            titleTextView = itemView.findViewById(R.id.title_text);
            summaryTextView = itemView.findViewById(R.id.summary_text);
            timeTextView = itemView.findViewById(R.id.time_text);
        }
    }
} 